package com.nexgo.payment.trans.setting.signature;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.nexgo.payment.R;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.trans.setting.SaveParameterActivity;
import com.nexgo.payment.trans.setting.SaveParameterPresenter;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.SwitchButton;
import com.nexgo.payment.widget.TitleBuilder;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 电子签名设置
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年7月6日
 */
public class SetupSignatureActivity extends SaveParameterActivity implements SwitchButton.OnCheckedChangeListener {
    private static final String TAG = SetupSignatureActivity.class.getSimpleName();

    @BindView(R.id.check_elec_signature)
    SwitchButton checkElecSignature;
    @BindView(R.id.check_confirm_signature)
    SwitchButton checkConfirmSignature;
    @BindView(R.id.signature_time_out)
    EditText signatureTimeOut;
    @BindView(R.id.signature_upload_times)
    EditText signatureUploadTimes;
    @BindView(R.id.max_repeat_signature)
    EditText maxRepeatSignature;
    @BindView(R.id.check_need_phone_number)
    SwitchButton checkNeedPhoneNumber;
    @BindView(R.id.layout_show_detail)
    LinearLayout layoutShowDetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_signature);
        ButterKnife.bind(this);
        new SaveParameterPresenter(this, Repository.getInstance());

        initTitle();
    }

    //初始化标题栏
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setRightText(getString(R.string.action_save))
                .setTitleText(getString(R.string.setup_signature_title))
                .build();
    }

    @Override
    protected HashMap<String, String> getDataBind() {
        HashMap<String, String> map = new HashMap<>();
        map.put(PreferencesConstants.SIGNATURE_TIME_OUT, signatureTimeOut.getText().toString());
        map.put(PreferencesConstants.SIGNATURE_UPLOAD_TIMES, signatureUploadTimes.getText().toString());
        map.put(PreferencesConstants.MAX_SIGNATURE_TIMES, maxRepeatSignature.getText().toString());
        map.put(PreferencesConstants.IS_SUPPORT_ELEC_SIGNATURE, checkElecSignature.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.IS_SHOW_CONFIRM_SIGNATURE, checkConfirmSignature.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.IS_NEED_PHONE_NUMBER, checkNeedPhoneNumber.isChecked() ? "1" : "0");
        return map;
    }

    @Override
    public void showDetail() {
        //监听是否支持电子签名
        checkElecSignature.setOnCheckedChangeListener(this);

        signatureTimeOut.setText(mPresenter.getParamValue(PreferencesConstants.SIGNATURE_TIME_OUT));
        signatureUploadTimes.setText(mPresenter.getParamValue(PreferencesConstants.SIGNATURE_UPLOAD_TIMES));
        maxRepeatSignature.setText(mPresenter.getParamValue(PreferencesConstants.MAX_SIGNATURE_TIMES));

        checkElecSignature.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.IS_SUPPORT_ELEC_SIGNATURE)));
        checkConfirmSignature.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.IS_SHOW_CONFIRM_SIGNATURE)));
        checkNeedPhoneNumber.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.IS_NEED_PHONE_NUMBER)));

        //判断是否支持电子签名
        isSupportSignature(checkElecSignature.isChecked());
    }

    @OnClick({R.id.title_iv_left, R.id.title_tv_right})
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.title_iv_left:
                finish();
                break;
            case R.id.title_tv_right:
                if (mPresenter != null) {
                    mPresenter.saveParams(getDataBind());
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(SwitchButton view, boolean isChecked) {
        switch (view.getId()){
            case R.id.check_elec_signature:
                isSupportSignature(isChecked);
                break;
            default:
                break;
        }
    }

    /**
     * 是否支持电子签名：支持-显示详情，不支持-关闭详情
     *
     * @param isSupport
     */
    private void isSupportSignature(boolean isSupport) {
        layoutShowDetail.setVisibility(isSupport ? View.VISIBLE : View.INVISIBLE);
    }

    public static Intent newInstance(Context context) {
        return new Intent(context, SetupSignatureActivity.class);
    }

}
